package com.example.demo.controller;

import com.example.demo.entity.Room;
import com.example.demo.service.IRoomService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class RoomController {

    @Autowired
    private IRoomService roomService;

    @RequestMapping("/roomList")
    public String list(Model model,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Room> roomList = roomService.selectAll();
        PageInfo pageInfo=new PageInfo(roomList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("roomList", roomList);

        return "admin/room/roomList";
    }

    @RequestMapping("/toEditRoom")
    public String toedit(Model model, Integer id) {

        if (id != null) {
            Room room = roomService.selectById(id);
            model.addAttribute("room", room);
        }

        return "admin/room/edit";
    }

    @RequestMapping("/editRoom")
    public String edit(Room room) {

        if (room.getId() != null ) {
            roomService.update(room);
        } else {
            roomService.insert(room);
        }

        return "redirect:roomList";
    }

    @RequestMapping("/deleteRoom")
    public String delete(Integer id) {

        roomService.delete(id);

        return "redirect:roomList";
    }

}
